/**
 * @file codeforces/1385/e/main
 * @brief
 * @see https://codeforces.com/contest/1385/submission/157377082
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/5/16 17:23:43
 **/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;

vi ord, used;
vector<vi> g;

void dfs(int v) {
  used[v] = 1;
  for (auto to : g[v])
    if (!used[to]) dfs(to);
  ord.push_back(v);
}

int main() {
  // High rating and good luck!
  int tc;
  cin >> tc;
  while (tc--) {
    int n, m;
    cin >> n >> m;
    g = vector<vector<int>>(n);
    vector<pair<int, int>> edges;
    for (int i = 0; i < m; ++i) {
      int t, x, y;
      cin >> t >> x >> y;
      --x, --y;
      if (t == 1) g[x].push_back(y);
      edges.emplace_back(x, y);
    }
    ord.clear();
    used = vector<int>(n);
    for (int i = 0; i < n; ++i)
      if (!used[i]) dfs(i);
    vector<int> pos(n);
    reverse(ord.begin(), ord.end());  // dfs 倒序就是一个拓扑序
    for (int i = 0; i < n; ++i) pos[ord[i]] = i;
    bool bad = false;
    for (int i = 0; i < n; ++i) {
      for (auto j : g[i])
        if (pos[i] > pos[j]) {
          bad = true;
          break;
        }
    }
    if (bad)
      cout << "NO" << endl;
    else {
      cout << "YES" << endl;
      for (auto [x, y] : edges) {
        if (pos[x] < pos[y])
          cout << x + 1 << ' ' << y + 1 << endl;
        else
          cout << y + 1 << ' ' << x + 1 << endl;
      }
    }
  }
  return 0;
}
